import Mock, { Random } from 'mockjs'
import { setPageData, saveData, delData } from '../common-mock-method'

Random.extend({
    phone: function () {
        const phonePrefixs = ['132', '135', '189'] // 自己写前缀哈
        return this.pick(phonePrefixs) + Mock.mock(/\d{8}/) // Number()
    },
    roles: function () {
        return this.pick(['超级管理员', '教师', '普通用户'])
    }
})
let pageData = Mock.mock({
    'list|19': [
        {
            'id|+1': 2,
            createTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
            updateTime: '@datetime(yyyy-MM-dd HH:mm:ss)',
            username: '@word(5,8)',
            nickName: '@cname()',
            realName: '@cname()',
            phone: '@phone(11)',
            remarks: '@cword(20,30)',
            roleAndPermissions: null,
            'roleIds|1-3': '@range(3-7)',
            'roleNames': ['@roles()']
        }
    ]
})
module.exports = [
    {
        url: '/mock/sys/user/page',
        method: 'post',
        response: (config) => {
            const pageList = {
                list: [
                    {
                        id: 1,
                        createTime: '2023-11-20 09:19:06',
                        updateTime: '2023-11-20 09:19:06',
                        username: 'admin',
                        nickName: '超级管理员',
                        realName: '超级管理员',
                        phone: 13566666666,
                        roleAndPermissions: null,
                        roleIds: [1],
                        roleNames: ['超级管理员'],
                        sysRoles: [
                            {
                                id: 1,
                                createTime: '2023-11-20 09:24:23',
                                updateTime: '2023-11-20 09:24:23',
                                createUser: null,
                                updateUser: null,
                                remarks: null,
                                delFlag: 0,
                                newRecord: false,
                                roleName: '超级管理员',
                                roleKey: 'ROLE_ADMIN'
                            }
                        ]
                    },
                    ...pageData.list]
            }
            return setPageData(pageList, config, 'username', 'nickName')
        }
    },
    {   // 保存
        url: '/mock/sys/user/save',
        method: 'post',
        response: (config) => {
            let requestBody = {body: config.body}
            let index
            if (requestBody.body.userId) {
                index = pageData.list.findIndex(({id}) => id === requestBody.body.userId)
            } else {
                index = pageData.list.findIndex(({id}) => id === requestBody.body.id)
            }
            if (index > -1 && requestBody.body.userId) {
                requestBody.body.id = requestBody.body.userId
                requestBody.body.username = pageData.list[index].username
                requestBody.body.realName = pageData.list[index].realName
                requestBody.body.nickName = pageData.list[index].nickName
                requestBody.body.phone = pageData.list[index].phone
            }
            return saveData(requestBody, pageData)
        }
    },
    {   // 删除
        url: RegExp('/mock/sys/user/del/' + '.*'),
        method: 'post',
        response: (config) => {
            return delData(config, '/mock/sys/user/del/', pageData, [1])
        }
    }
]
